home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3248 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: Rezonet.net!news
  2. From: ray@ultimate-tech.com (Ray Dunn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Weird malloc() seg-faults [Need Help!]
  5. Date: 24 Jan 1996 17:44:36 GMT
  6. Organization: Ultimate Technographics Inc.
  7. Message-ID: <4e5r64$1587@ns.RezoNet.NET>
  8. References: <4drhfg$m9s@usenet.srv.cis.pitt.edu>
  9. NNTP-Posting-Host: 204.19.230.7
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. Keywords: malloc, weird, segmentation faults, bad programming?, help
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In referenced article, Jayadev Billa says...
  16. >I have been having problems with seg-faults with malloc.
  17. >....
  18. >CreateVector is a simple call to calloc as:
  19. >
  20. >typedef float*  Vector;
  21. >
  22. >Vector CreateVector(int size)
  23. >{
  24. >  Vector vec;
  25. >
  26. >  if ((vec = (Vector) calloc(size+1,sizeof(float)))==NULL)
  27. >    WError(10,"Unable to allocate memory for Vector");
  28. >  *vec = (float) size;
  29. >  return(vec);
  30. >}
  31. >
  32. >void FreeVector(Vector vec)
  33. >{
  34. >  free(vec);
  35. >}
  36.  
  37. Clearly there is nothing wrong with the way you allocate and free the 
  38. memory for your vectors, except (nothing to do with your problem):
  39.  
  40. - malloc rather than calloc would be sufficient and more efficient,
  41. - depending on the overhead per alloced cell on your system, this might 
  42.   make very inefficient use of memory if you have a large number of 
  43.   Vectors.
  44.  
  45. You problem must lie somewhere else in your program.  Somewhere you are 
  46. writing beyond the limits of an allocated cell and thus corrupting the 
  47. alloc heap space.  These bugs are often difficult to find - try to get 
  48. hold of a memory checking alloc package.
  49. -- 
  50. Ray Dunn (opinions are my own) | Phone: (514) 938 9050
  51. Montreal                       | Phax : (514) 938 5225
  52. ray@ultimate-tech.com          | Home : (514) 630 3749
  53.  
  54.